package com.micrometa.km.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class PropertyUtil {
	private static final String config = "config.properties";

	private static Properties configInput() {
		Properties propertie = new Properties();
		try {
			FileInputStream inputFile = new FileInputStream(config);
			propertie.load(inputFile);
			inputFile.close();
		} catch (FileNotFoundException e) {
			System.out.println("读取属性文件--->失败！- 原因：文件路径错误或者文件不存在");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("装载文件--->失败!");
			e.printStackTrace();
		}
		return propertie;
	}
	private static Properties configOutput(Properties propertie){
		try {
			FileOutputStream outputFile = new FileOutputStream(config);
			propertie.store(outputFile, "Modified by user from application:");
			outputFile.close();
		} catch (FileNotFoundException e) {
			System.out.println("写入属性文件--->失败！- 原因：文件路径错误或者文件不存在");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("装载文件--->失败!");
			e.printStackTrace();
		}
		return propertie;
	}

	private static String read(String param) {
		Properties propertie = configInput();
		if (propertie.containsKey(param)) {
			return propertie.getProperty(param);// 得到某一属性的值
		} else
			return "";
	}

	private static void write(String param, String value) {
		Properties propertie = configInput();
		propertie.setProperty(param, value);
		configOutput(propertie);
	}

	public static String repositoryRoot() {
		return read("root");
	}

	public static void repositoryRootConfig(String value) {
		write("root", value);
	}
	
	public static String skin(){
		return read("skin");
	}
	public static void skinConfig(String value){
		write("skin",value);
	}

	public static String action(){
		return read("action");
	}
	public static void actionConfig(String value) {
		write("action",value);
	}
	public static void main(String[] args) {
		
		repositoryRootConfig("repository");
		skinConfig("ginimi");
//		System.out.println(repositoryRoot());
//		System.out.println(skin());
	}
}
